package com.cloudcc.mobile.view.dynamic;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;

import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.ChatAtAdapter;
import com.cloudcc.mobile.db.ChatMessageDB;
import com.cloudcc.mobile.entity.chat.ChatMessage;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.NotificationUtils;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.weight.CustomListView;
import com.lidroid.xutils.exception.DbException;
import com.litesuits.android.log.Log;

/**
 * 涉及我的更新
 * 
 * @author Administrator
 * 
 */
public class AboutMeListActivty extends BaseActivity {
	private CustomListView listview;
	private ImageView back;
	private ChatAtAdapter adapter;
	private ChatMessageDB db;
	private List<ChatMessage> list = new ArrayList<ChatMessage>();;

	@Override
	public int getLayoutId() {
		return R.layout.chat_guanyu_me;
	}

	@Override
	public void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		try {
			setUpView();
			addListener();
		} catch (DbException e) {
			Tools.handle(e);
		}
	}

	private void addListener() {
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AboutMeListActivty.this.finish();
			}
		});
		// 长按
		listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				menu.add(1, 100, 1, "删除消息");
				menu.add(1, 101, 2, "清空消息");
			}
		});
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ChatMessage message = adapter.getItem(arg2 - 1);
				Intent intent = new Intent(mContext, DynamicActivity.class);
				intent.putExtra("chatId", message.getRelatedId());
				startActivity(intent);
				try {
					db.deleteById(message.getId());
					NotificationUtils.clearNotification(mContext, 2004);
					Intent messageNum = new Intent(
							AppContext.MESSAGE_SYSTEM_NUM);
					mContext.sendBroadcast(messageNum);
				} catch (DbException e) {
					e.printStackTrace();
				}
			}
		});
	}

	// 上下文菜单
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		try {
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
					.getMenuInfo();
			ChatMessage message = adapter.getItem(info.position - 1);
			switch (item.getItemId()) {
			case 100:
				db.deleteById(message.getId());
				list = db.getList();
				adapter.changeData(list);
				NotificationUtils.clearNotification(mContext, 2004);
				Intent messageNum = new Intent(AppContext.MESSAGE_SYSTEM_NUM);
				mContext.sendBroadcast(messageNum);
				break;
			case 101:
				db.deleteChatMessageAll();
				list = db.getList();
				adapter.changeData(list);
				NotificationUtils.clearNotification(mContext, 2004);
				Intent messageNum2 = new Intent(AppContext.MESSAGE_SYSTEM_NUM);
				mContext.sendBroadcast(messageNum2);
				break;
			default:
				break;
			}
		} catch (DbException e) {
			Tools.handle(e);
		}
		return super.onContextItemSelected(item);
	}

	private void setUpView() throws DbException {
		listview = (CustomListView) findViewById(R.id.guanyu_me);
		back = (ImageView) findViewById(R.id.return_back);
		db = new ChatMessageDB(this, UserManager.getManager().getUser().userId
				+ "chat");
		list = db.getList();
		adapter = new ChatAtAdapter(this, list);
		listview.setAdapter(adapter);
	}

	@Override
	protected void onRestart() {
		try {
			list = db.getList();
			if (list.size() != 0) {
				adapter.changeData(list);
			} else {
				finish();
			}
		} catch (DbException e) {
			e.printStackTrace();
		}
		super.onRestart();
	}
}
